package com.ostrich.a2104gao5.test22

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/17 13:13
 * @Description : MagicBox2
 */
class MagicBox2<T>(item:T) {
    var available = false
    private var subject:T = item
    //泛型函数，泛型作为输出
    fun fetch():T? {
        //true返回subject false返回null
        return subject.takeIf { available }
    }
}
class Boy2(val name:String,val age:Int)
class Dog2(val weight:Int)
fun  main(){
    val box1:MagicBox2<Boy2> = MagicBox2(Boy2("占三",34))
    val box2:MagicBox2<Dog2> = MagicBox2(Dog2(20))
    box1.available = true
    box1.fetch()?.run {
        println("${this.name}")
    }
}